/*
 * Hello.cpp
 *
 *	A "Hello World" of sorts. Make sure you can compile and run this before we move on.
 * 	It should output "Everything is working as intended" after you close the window.
 *
 * 	All functionality will be tested here to make sure it works.
 *
 *  Created on: Apr 26, 2013
 *      Author: zapmunk
 */


#include "headerstop.h"

#include "Resource.h"
#include "TextResource.h"
#include "PNGResource.h"

#include "ResourceLoader.h"

#include "GLLib.h"

using namespace std;

const uint DISPLAY_WIDTH = 640, DISPLAY_HEIGHT = 480;

int main(int argc, char* argv[]) {
	commandeerStdout();

	ResourceLoader::initResources();
	ResourceLoader::loadPreload();
	cout << "Loaded preload resources!" << endl;

	if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) {
		exitWithMessage(EXIT_FAILURE, SDL_GetError());
	}
	cout << "SDL init successful!" << endl;

	if (SDL_GL_LoadLibrary(NULL) < 0) {;
		exitWithMessage(EXIT_FAILURE, SDL_GetError());
	}
	cout << "Loaded OpenGL libs!" << endl;

	if (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0) < 0) {
		exitWithMessage(EXIT_FAILURE, SDL_GetError());
	}
	cout << "GL 2D-mode successful!" << endl;

	SDL_Window* window;
	if (!(window = SDL_CreateWindow("Ascension (dev engine test yay)",
				SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
				DISPLAY_WIDTH, DISPLAY_HEIGHT,
				SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL))) {
		exitWithMessage(EXIT_FAILURE, SDL_GetError());
	}
	cout << "Window creation successful!" << endl;

	SDL_GLContext glContext = NULL;
	if (!(glContext = SDL_GL_CreateContext(window))) {
		exitWithMessage(EXIT_FAILURE, SDL_GetError());
	}
	cout << "Created OpenGL context!" << endl;

	if (!GL::initGL(window, &glContext)) {
		exitWithMessage(EXIT_FAILURE, SDL_GetError());
	}
	cout << "Loaded OpenGL callbacks!" << endl;

	if (IMG_Init(IMG_INIT_PNG) < 0) {
		exitWithMessage(EXIT_FAILURE, IMG_GetError());
	}
	cout << "IMG init successful!" << endl;

	cout << "Initializing GL shiz!" << endl;
	GL::glMatrixMode(GL_PROJECTION|GL_MODELVIEW);
	GL::glLoadIdentity();
	GL::glOrtho(0, DISPLAY_WIDTH, DISPLAY_HEIGHT, 0, 0, 1);
	GL::glDisable(GL_DEPTH_TEST);
	GL::glEnable(GL_TEXTURE_2D);
	GL::glEnable(GL_BLEND);
	GL::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	ResourceLoader::loadAutoload();
	cout << "Loaded autoload resources!" << endl;

	cout << "Testing resource loading... " << flush;
	if (ResourceLoader::testpng.load()) {
		cout << "Success!";
	} else {
		cout << "Failure!";
		exitWithMessage(EXIT_FAILURE, "Could not load the resource!");
	}
	cout << endl;
	SDL_Surface* testimg = ResourceLoader::testpng.getImage();
	if (!testimg) {
		ResourceLoader::testpng.unload();
		cout << "Could not get image!" << endl;
		exitWithMessage(EXIT_FAILURE, IMG_GetError());
	}

	Texture testtex(testimg);
	cout << "Loaded test texture!" << endl;

	SDL_FreeSurface(testimg);

	cout << "All systems nominal!" << endl;

	double theta = 0;

	bool running = true;
	while (running) {
		SDL_Event e;
		while (SDL_PollEvent(&e)) {
			switch (e.type) {
			case SDL_QUIT:
				cout << "Terminating..." << endl;
				running = false;
				break;
			default:
				break;
			}
		}

		GL::glClearColor(0, 0, 0, 1);
		GL::glClear(GL_COLOR_BUFFER_BIT);

		GL::glPushMatrix();
		rotateAround(theta, 64+32, 64+32);
		GL::drawTexture(testtex, 64, 64);
		GL::glPopMatrix();

		GL::glPushMatrix();
		rotateAround(-(theta*.8), 96+32, 64+32);
		GL::drawTexture(testtex, 80, 64);
		GL::glPopMatrix();

		SDL_GL_SwapWindow(window);

		theta += 0.05;
	}

	cout << "Cleaning up Resource shiz!" << endl;
	ResourceLoader::unloadAll();

	cout << "Cleaning up GL shiz!" << endl;
	SDL_GL_DeleteContext(glContext);
	testtex.clean();

	cout << "Cleaning up SDL shiz!" << endl;
	IMG_Quit();
	SDL_Quit();


	//2 unfreed blocks of 48 and 140 bytes in dbgout.cpp are EXPECTED, and will be freed shortly.
	if (GetUnfreedMemory() != 188) {
		PrintMemoryLeakInfo();
	} else {
		cout << ">INFO\tNo memory leaks detected" << endl;
	}


	releaseStdout();
	exitWithMessage(EXIT_SUCCESS, "Everything is working as intended!");
	return EXIT_SUCCESS; //redundant :P
}
